package com.buydeem.share;

/**
 * wait和notify基本使用
 *
 * @author zengchao
 * @date 2021-07-21 10:31:31
 * @since 1.0
 */
public class WaitNotifyDemo2 {
    static final Object LOCK = new Object();
    static Integer number = 0;

    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () -> {
            for (int i = 0; i < 1000; i++) {
                synchronized (LOCK){
                    LOCK.notify();
                    number++;
                    System.out.printf("线程:[%s]打印number:[%d]%n",Thread.currentThread().getName(),number);
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Thread t1 = new Thread(runnable);
        t1.setName("t1");

        Thread t2 = new Thread(runnable);
        t2.setName("t2");

        t1.start();
        t2.start();

        t1.join();
        t2.join();
    }
}
